﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityObjects;
using BAL.Controller;

namespace PresentationLayer.View.DepartmentEmployee
{
    public partial class SelectStationeryItems : System.Web.UI.Page
    {
        private static MakeRequisitionControl makeRequisitionControl = new MakeRequisitionControl();
        private static Requisition req;       
        private static List<RequisitionDetails> selectedItems = new List<RequisitionDetails>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                req = (Requisition)Session["Requisition"];
                DataBindCategory();
                DataBindCatalouge();
            }
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataBindCatalouge();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //-------------Updating----------
            req.ReqDetails_list = selectedItems;
            Response.Redirect("StationeryRequisitionForm.aspx");
        }

        private void DataBindCatalouge()
        {
            int categoryId = Convert.ToInt32(ddlCategory.SelectedValue);
            if (categoryId == 0)
            {
                GridView2.DataSource = makeRequisitionControl.getAllCatalogue();
            }
            else
            {
                GridView2.DataSource = makeRequisitionControl.getCatalogueByCategoryId(categoryId);
            }
            GridView2.DataBind();
            foreach (GridViewRow row in GridView2.Rows)
            {
                foreach (RequisitionDetails reqDetails in selectedItems)
                {
                    if (reqDetails.ItemId == ((Label)row.FindControl("lblItemId")).Text)
                    {
                        ((CheckBox)row.FindControl("chkCheckToAdd")).Checked = true; ;
                    }
                }
            }
        }

        private void DataBindCategory()
        {
            ddlCategory.DataSource = makeRequisitionControl.getAllCategory();
            ddlCategory.DataBind();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/View/DepartmentEmployee/StationeryRequisitionForm.aspx");
        }

        protected void chkCheckToAdd_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkToAdd = (CheckBox)sender;            
            string itemId = ((Label)chkToAdd.Parent.FindControl("lblItemId")).Text.Trim();
            string itemName = ((Label)chkToAdd.Parent.FindControl("lblDescription")).Text.Trim();
            int availableQty = Convert.ToInt32(((Label)chkToAdd.Parent.FindControl("lblAvailableQty")).Text.Trim());
            //////Update selectedItem/////
            foreach (RequisitionDetails selecteditem in selectedItems)
            {
                if (selecteditem.ItemId == itemId)
                {
                    if (!chkToAdd.Checked) selectedItems.Remove(selecteditem);
                    return;
                }
            }
            if (chkToAdd.Checked)
            {
                RequisitionDetails reqDetails = new RequisitionDetails();
                reqDetails.ItemId = itemId;
                reqDetails.ItemName = itemName;
                reqDetails.OutStandingQty = availableQty;
                reqDetails.RequestedQty = 1;

                selectedItems.Add(reqDetails);
            }
        }        

    }
}
